#!/usr/bin/env perl
use warnings;
use strict;

# A simple diff on rank files. This is a simple measure of the difference
# between rank files generated with different parameters. The ultimate measure
# is of course their prediction performance, but perhaps the information
# computed here is also interesting. 
# Takes one argument: a list of rank files (or any files, really), one file
# name per line, to compare.
# Output is a list of the form
# file1:file2:length_of_diff

scalar(@ARGV) == 1 or die "need list of files to analyze as argument";
my $input_file_list = $ARGV[0];
-e$input_file_list or die "input file list file $input_file_list not found";

open(my $fh, "<$input_file_list");
my @files = <$fh>;
close($fh);

scalar(@files) > 0 or die "no files in input file list";
foreach (@files) {chomp}

# Should get the same number of lines in each file.
#my $line_count = -1;
#foreach my $fn (@files) {
#  next unless -e$fn;
#  `wc -l $fn` =~ /(\d+).*/ or die "unexpected output from wc -l";
#  if ($line_count > 0 && $1 != $line_count) {
#    print "file $fn has unexpected number of lines ($1)\n";
#  }
#  $line_count = $1;
#}
`wc -l $files[0]` =~ /(\d+).*/ or die "unexpected output from wc -l";
my $line_count = $1;

# Count differences.
for (my $f0 = 0; $f0 < scalar(@files); ++$f0) {
  my $fn0 = $files[$f0];
  next unless -e$fn0;
  for (my $f1 = $f0+1; $f1 < scalar(@files); ++$f1) {
    my $fn1 = $files[$f1];
    next unless -e$fn1;
    system("make $fn0.page_numbers");
    system("make $fn1.page_numbers");
    my $mismatched_lines = 
      `diff -U 0 $fn0.page_numbers $fn1.page_numbers | wc -l`;
    print "$fn0:$fn1:$mismatched_lines";
    #$fn =~ /.+_(.*)_n(.+)_e(.*).*/ or die "can't parse file name $fn";
    #my $i = $1;
    #my $n = $2;
    #my $e = $3;
  }
}
# Copyright (c) 2009 John Lees-Miller
# 
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

